/**
 * 用户详情对话框（可用于添加和修改对话框）
 */
var OrderInfoDlg = {
    width: '650px',
    height: 'auto',
    currentLayerIndex: -1,
    cache: "",
    InfoData: {},
    courseTableData:[]
};

/**
 * 初始化新增弹出框
 */
OrderInfoDlg.init = function () {
    this.cache = $("#orderDialog").html();
    $("#orderDialog").html('');
};

/**
 * 清除数据
 */
OrderInfoDlg.clearData = function () {
    this.InfoData = {};
    this.courseTableData = [];
};

/**
 * 关闭此对话框
 */
OrderInfoDlg.close = function () {
    layer.close(this.currentLayerIndex);
};

/**
 * 新增打开模态框调用事件
 */
OrderInfoDlg.renderAdd = function () {
    //初始化icheck选中样式
    initICheck();
};


/**
 * 编辑
 */
OrderInfoDlg.load = function (id) {
    var ajax = new $ax(Feng.ctxPath+"/order/getById", function(data){
        Feng.log("订单信息查询成功");
        if (data.success){
            var order = data.obj || null;
            //显示订单信息
            for (var i in order) {
                //判断Id与name是否相同
                if ($("#" + i).attr("name") === i) {
                    if(i == "discountAmount"){
                        $("#" + i).html(numeral(order[i]).format('0,0.00'));
                    }else{
                        $("#" + i).html(order[i]);
                    }
                }
            }

            //订单状态
            if(order.orderState==2){
                $('#orderState').html("已支付");
            }else if(order.orderState==4){
                $('#orderState').html("已取消");
            }else {
                $('#orderState').html("未支付");
            }

            //实际付款
            $("#actualAmount span").html(numeral(order.actualOrderAmount).format('0,0.00'));
            //退款金额
            $("#refundsAmount span").html(numeral(order.refundsAmount).format('0,0.00'));
            //订单金额
            $("#orderAmout span").html(numeral(order.actualOrderAmount - order.refundsAmount).format('0,0.00'));
            //订单状态显示
            if(order.orderState==2){
                $("#payAmont button").css("display","none");
                $("#payAmont span").css("display","");
            }else if(order.payment == "1"&&order.orderState==1){
                $("#payAmont button").attr("value",order.orderNum);
            }else if(order.payment == "2"&&order.orderState==1){
                $("#payAmont button").attr("value",order.orderNum);
            }else if(order.payment == "3"&&order.orderState== 1 ){
                $("#payAmont button").attr("value",order.orderNum);
            }else{
                $("#payAmont button").css("display","none");
                $("#payAmont span").css("display","");
            }

            //显示课程信息
            var totalDiscountPrice = 0;
            //订单总价（所有课程总价，不包括优惠券价格）
            var orderAmount = 0;
            if (order !== null) {
                $.each(order.orderItems,function (i,item) {
                    item.orderState = order.orderState;
                    //课程实际价格
                    var tCourseAmount = item.courseAmount;
                    item.courseAmount = numeral(tCourseAmount).format('0,0.00');
                    // 课程单价
                    item.coursePrice = numeral(item.coursePrice).format('0,0.00');
                    //课程总价
                    item.course.totalPrice = numeral(item.course.totalPrice).format('0,0.00');

                    item.course.discountPrice = numeral(item.course.discountPrice).format('0,0.00');
                    item.itemStatus = changeCourseMark(item.itemStatus);
                    item.refundStatus = refundStatusMark(item.refundStatus);
                    totalDiscountPrice += item.course.discountPrice;
                    item.userId = order.userId;

                    //活动减免
                    item.promotionAmount = numeral(item.promotionAmount).format('0,0.00');
                    switch (item.itemStatus) {
                        case '1' : {
                            item.itemStatus = '未转'
                        }
                            break;
                        case '2' : {
                            item.itemStatus = '已转出'
                        }
                            break;
                        case '3' : {
                            item.itemStatus = '新转入'
                        }
                            break;
                        case '4' : {
                            item.itemStatus = '新转出'
                        }
                            break;
                        default:
                    }
                    orderAmount += parseFloat(tCourseAmount);
                    totalDiscountPrice += item.promotionAmount;

                });
                $("#orderAmount").html(numeral(orderAmount).format('0,0.00'));
                var orderImtemsList = order.orderItems;
                OrderInfoDlg.courseTableData = orderImtemsList;
                OrderInfoDlg.refresh();
            }
            //总活动减免
            // $("#totalDiscountPrice span").html(numeral(totalDiscountPrice).format('0,0.00'));
            //现金支付
            $("#payCrash").click(function () {
                var orderState = order.orderState;
                var orderNum=$(this).attr("value");
                var couponCardNo = order.couponCardNumber;
                //待支付
                if(orderState == 1){
                    var ajax = new $ax(Feng.ctxPath+"/order/updateOrderState", function(data){
                        if (data.success){
                            Feng.success("支付成功！");
                            $("#payAmont span").css("display","");
                            $("#payAmont span").html("已支付");
                            $('#orderState').html("已支付");
                            $("#payAmont button").css("display","none");
                            OrderTable.me.refresh();
                        } else if (data.code === 2) {
                            Feng.error(data.msg);
                        } else {
                            Feng.error("更新失败!");
                        }
                    },function(data){
                        Feng.error("更新失败!");
                    });
                    ajax.data={orderNum:orderNum,orderState:2,payment:1};
                    ajax.start();
                    //退款中
                }

            })
            //公司转账
            $("#payCompany").click(function () {
                var orderState = order.orderState;
                var orderNum=$(this).attr("value");
                var couponCardNo = order.couponCardNumber;
                //待支付
                if(orderState == 1){
                    var ajax = new $ax(Feng.ctxPath+"/order/updateOrderState", function(data){
                        if (data.success){
                            Feng.success("支付成功！");
                            $("#payAmont span").css("display","");
                            $("#payAmont span").html("已支付");
                            $('#orderState').html("已支付");
                            $("#payAmont button").css("display","none");
                            OrderTable.me.refresh();
                        } else if (data.code === 2) {
                            Feng.error(data.msg);
                        } else {
                            Feng.error("更新失败!");
                        }
                    },function(data){
                        Feng.error("更新失败!");
                    });
                    ajax.data={orderNum:orderNum,orderState:2,payment:3};
                    ajax.start();
                    //退款中
                }

            })

        }
    },function(data){
        Feng.error("加载信息失败!");
    });
    ajax.data={id:id};
    ajax.start();
};

/**
 * 转班状态标识
 * @param itemStatus
 * @returns {string}
 */
function changeCourseMark(itemStatus) {
    var mark;
    switch (itemStatus) {
        case '1' : {
            mark = '未转'
        }
            break;
        case '2' : {
            mark = '已转出'
        }
            break;
        case '3' : {
            mark = '新转入'
        }
            break;
        case '4' : {
            mark = '新转出'
        }
            break;
        default:
    }
    return mark;
}

/**
 * 退款状态标识
 * @param refundStatus
 * @returns {string}
 */
function refundStatusMark(refundStatus) {
    var mark;
    switch (refundStatus) {
        case '1' : {
            mark = '正常'
        }
            break;
        case '2' : {
            mark = '退款审核中'
        }
            break;
        case '3' : {
            mark = '已退款'
        }
            break;
        default:
            mark = '正常'
    }
    return mark;
}

/**
 * 跳转至转班页面
 */
OrderInfoDlg.changeCourseDlg = function (orderItemId, userId) {
    var index = layer.open({
        type: 1,
        title: '请选择课程',
        skin: 'layui-layer-lan',
        area: [ChangeCourseDlg.width, ChangeCourseDlg.height],
        content: ChangeCourseDlg.cache,
        success: function (layero, index) {
            //层弹出成功后回调方法
            ChangeCourseDlg.orderItemId = orderItemId;
            ChangeCourseDlg.userId = userId;
            ChangeCourseDlg.initTable();
        },
        maxmin: true,
        scrollbar: false,
        btnAlign: 'r',  //默认值
        btn2: function (index) {
            layer.close(index);
        },
        cancel: function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    layer.full(index);
    ChangeCourseDlg.currentLayerIndex = index;
};
/**
 * 打开调整活动金额对话框
 */
OrderInfoDlg.reducePromotionAmountDlg = function (orderNum,orderItemId,courseName,courseAmount,_index) {
    var index = layer.open({
        type: 1,
        title: '请填写调整的金额',
        skin: 'layui-layer-lan',
        area: [ReduceAmountDlg.width, ReduceAmountDlg.height],
        content: ReduceAmountDlg.cache,
        success: function (layero, index) {
            //层弹出成功后回调方法
            ReduceAmountDlg.orderItemId = orderItemId;
            ReduceAmountDlg.reducePromotionAmount(orderNum,orderItemId,courseName,courseAmount);
        },
        maxmin: true,
        scrollbar: false,
        btn:['保存','关闭'],
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            ReduceAmountDlg.reducePromotionAmountSubmit(_index);
            layer.close(index);
            OrderTable.me.refresh();
            //layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        btn2: function (index) {
            layer.close(index);
        },
        cancel: function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    // layer.full(index);
    ReduceAmountDlg.currentLayerIndex = index;
};

//刷新列表
OrderInfoDlg.refresh = function () {
    var outHtml = template("dataCourseContentTmpl", this.courseTableData);
    $("#courseTableBody").html(outHtml);
};